package com.fish.yssb.ui.page.activity.constPara;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fish.yssb.R;
import com.fish.yssb.db.RealmDb;
import com.fish.yssb.db.bean.BaseInfoTable;
import com.fish.yssb.ui.page.activity.base.BaseActivity;
import com.fish.yssb.ui.page.adapter.constParaAdapter;
import com.fish.yssb.ui.page.adapter.realtimeYCListAdapter;
import com.fish.yssb.ui.page.dialog.PublicNoticeDialog;
import com.fish.yssb.ui.page.util.TopToastUtil;
import com.jaeger.library.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * A login screen that offers login via email/password.
 */
public class ConstParaActivity extends BaseActivity implements OnClickListener {
    private static final String TAG = ConstParaActivity.class.getSimpleName();


    TextView tv_back,tv_toptitle,tv_set,tv_save,tv_load,tv_updata;

    private RecyclerView recyclerView;
    private constParaAdapter adapter;
    private  PublicNoticeDialog noticeDialog;
    private RelativeLayout ll_para_1, ll_para_2, ll_para_3;

    public static void start(Context context) {
        Intent intent = new Intent(context, ConstParaActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setTransparent(this);
        StatusBarUtil.setDarkMode(this);
        setContentView(R.layout.activity_const_para_list);
        initview();

    }




    //初始化视图
    @SuppressLint("WrongViewCast")
    private void initview() {
        //登录层、下拉层、其它登录方式层
        recyclerView = (RecyclerView) findViewById(R.id.recylerview);
        recyclerView.setHasFixedSize(true);

        tv_toptitle = findViewById(R.id.tv_toptitle);
        tv_toptitle.setText("定值参数");

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setAdapter(adapter = new constParaAdapter());

        adapter.replaceAll(getData());

        tv_back = findViewById(R.id.tv_back);
        tv_set = findViewById(R.id.tv_set);
        tv_load = findViewById(R.id.tv_load);
        tv_save = findViewById(R.id.tv_save);
        tv_updata = findViewById(R.id.tv_updata);
        tv_set.setOnClickListener(this);
        tv_back.setOnClickListener(this);
        tv_save.setOnClickListener(this);
        tv_load.setOnClickListener(this);
        tv_updata.setOnClickListener(this);

    }



    @Override
    public void onClick(View view) {


        switch (view.getId()) {

            case R.id.tv_updata:

                noticeDialog = new PublicNoticeDialog(ConstParaActivity.this, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String sendStatus = "";
                        if (view.getId() == R.id.btn_chancel_notice) {//暂存

                            noticeDialog.dismiss();
                        } else {//生成
                             ArrayList<BaseInfoTable> dataList = new ArrayList<BaseInfoTable>();
                                             dataList = adapter.dataList;
                                             for(int i=0;i<dataList.size();i++)
                                             {
                                                 if(!"".equalsIgnoreCase(dataList.get(i).getRealVal())) {
                                                     dataList.get(i).setDefualtVal(dataList.get(i).getRealVal());
                                                     dataList.get(i).setRealVal("");
                                                 }
                                             }
                                             RealmDb.getInstance().insertRealmObjects(dataList);
                                             adapter.notifyDataSetChanged();


                            noticeDialog.dismiss();

                        }
                        noticeDialog.dismiss();

                    }
                }, 1, "", "确认下装参数？", "取消", "确定");
                noticeDialog.show();


                break;
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_set:
                break;
            case R.id.tv_load:
                 adapter.replaceAll(getData());
                 adapter.notifyDataSetChanged();
                TopToastUtil.getInstance(this,R.layout.showdialog).showShortToast("加载成功");
                break;
            case R.id.tv_save:
                    ArrayList<BaseInfoTable> dataList = new ArrayList<BaseInfoTable>();
                                                                dataList = adapter.dataList;

                                                                RealmDb.getInstance().insertRealmObjects(dataList);
                                                                adapter.notifyDataSetChanged();

                TopToastUtil.getInstance(this,R.layout.showdialog).showShortToast("保存成功");
                break;
            default:
                break;
        }


    }

    public ArrayList<BaseInfoTable> getData()
    {
        List<BaseInfoTable> list = (List<BaseInfoTable>) RealmDb.getInstance().queryRealmObjects(BaseInfoTable.class);

        Log.e("=========","count:"+list.size());
        ArrayList<BaseInfoTable> arrayList = (ArrayList) RealmDb
                .getInstance()
                .queryRealmObjectsByData(BaseInfoTable.class,"paraType",2);

        return arrayList;
    }

}

